﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DIS_Client.Helpers
{
    #region Using
    using DevComponents.DotNetBar;
    using System.Windows.Forms;
    #endregion

    public class AllowFunctionsManager
    {
        /// <summary>
        /// Retrieve enum function from name
        /// </summary>
        /// <param name="chucnang"></param>
        /// <returns></returns>
        public static EnumChucNangHeThong RetrieveFuncsNotAllowUsing(string tenchucnang)
        {
            EnumChucNangHeThong func = EnumChucNangHeThong.NONE;
            if (tenchucnang == "Manage Roles - Functions")
            {
                func = EnumChucNangHeThong.MANAGE_ROLEFUNCTIONS;
            }
            else if (tenchucnang == "Chemist WareHouses")
            {
                func = EnumChucNangHeThong.MANAGE_ChemistWareHouse;
            }
            else if (tenchucnang == "Staffs")
            {
                func = EnumChucNangHeThong.MANAGE_STAFF;
            }
            else if (tenchucnang == "Categories")
            {
                func = EnumChucNangHeThong.MANAGE_CATEGORY;
            }
            else if (tenchucnang == "Items")
            {
                func = EnumChucNangHeThong.MANAGE_ITEM;
            }
            else if (tenchucnang == "Record Orders")
            {
                func = EnumChucNangHeThong.MANAGE_RECORDORDERS;
            }
            else if (tenchucnang == "Daily Report")
            {
                func = EnumChucNangHeThong.MANAGE_DAILYREPORT;
            }
            else if (tenchucnang == "Weekly Report")
            {
                func = EnumChucNangHeThong.MANAGE_WEEKLYREPORT;
            }
            else if (tenchucnang == "Promotion Discount")
            {
                func = EnumChucNangHeThong.MANAGE_PROMOTIONDISCOUNT;
            }
            return func;
        }

        /// <summary>
        /// Mapping name of controls with specifed enums so that enable or disable some functions for each users
        /// </summary>
        /// <returns></returns>
 
        public static Dictionary<string, EnumChucNangHeThong> InitFuncsMapping()
        {
            var dict = new Dictionary<string, EnumChucNangHeThong>();

            dict.Add("menManageFunctions", EnumChucNangHeThong.MANAGE_ROLEFUNCTIONS);
            dict.Add("menChemistWareHouses", EnumChucNangHeThong.MANAGE_ChemistWareHouse);
            dict.Add("menStaffs", EnumChucNangHeThong.MANAGE_STAFF);

            dict.Add("menCategories", EnumChucNangHeThong.MANAGE_CATEGORY);
            dict.Add("menItems", EnumChucNangHeThong.MANAGE_ITEM);
            dict.Add("ttbtnItems", EnumChucNangHeThong.MANAGE_ITEM);

            dict.Add("menRecordOrders", EnumChucNangHeThong.MANAGE_RECORDORDERS);
            dict.Add("ttbtnRecordOrders", EnumChucNangHeThong.MANAGE_RECORDORDERS);

            dict.Add("menDailyReports", EnumChucNangHeThong.MANAGE_DAILYREPORT);
            dict.Add("ttbtnDailyReports", EnumChucNangHeThong.MANAGE_DAILYREPORT);

            dict.Add("menWeeklyReports", EnumChucNangHeThong.MANAGE_WEEKLYREPORT);
            dict.Add("ttbtnWeeklyReports", EnumChucNangHeThong.MANAGE_WEEKLYREPORT);

            dict.Add("menPromotionDiscount", EnumChucNangHeThong.MANAGE_PROMOTIONDISCOUNT);
            dict.Add("ttbtnPromotionDiscount", EnumChucNangHeThong.MANAGE_PROMOTIONDISCOUNT);

            return dict;
        }

        public static List<EnumChucNangHeThong> InitFuncsAllowUsing()
        {
            var lstAllFuncs = BusService.Instance.Functions_SelectAll();
            var lstFuncsBelongToUser = BusService.Instance.RoleFunctions_SelectByIDRole(GlobalVariables.g_CurrentUser.IDRole);
            var lst = new List<EnumChucNangHeThong>();
            foreach (var item in lstAllFuncs)
            {
                foreach (var item2 in lstFuncsBelongToUser)
                {
                    if (item.ID == item2.IDFunction)
                    {
                        lst.Add(RetrieveFuncsNotAllowUsing(item.Name));
                        break;
                    }
                }
            }
            return lst;
        }

        private static void DisableCtrls(Panel pnlMain, string shortcut)
        {
            foreach (Control objControl in pnlMain.Controls)
            {
                if (objControl.GetType() == typeof(MenuStrip))
                {
                    foreach (ToolStripMenuItem _item in ((MenuStrip)objControl).Items)
                    {
                        DiableMenuItem(_item, shortcut);
                    }
                }
                else if (objControl.GetType() == typeof(ToolStrip))
                {
                    foreach (var _item in ((ToolStrip)objControl).Items)
                    {
                        if (_item.GetType() == typeof(ToolStripButton))
                        {
                            DiableToolStripItem((ToolStripButton)_item, shortcut);
                        }
                    }
                }
                else if (objControl.GetType() == typeof(ExplorerBar))
                {
                    foreach (ExplorerBarGroupItem _item in ((ExplorerBar)objControl).Groups)
                    {
                        DiableButtonItem(_item, shortcut);
                    }
                }
            }
        }

        private static void DiableButtonItem(ExplorerBarGroupItem parentItem, string shortcut)
        {
            if (parentItem != null && parentItem.SubItems.Count > 0)
            {
                foreach (var _item in parentItem.SubItems)
                {
                    if (_item.GetType() == typeof(ButtonItem))
                    {
                        if (shortcut == ((ButtonItem)_item).Name)
                        {
                            ((ButtonItem)_item).Enabled = true;
                            return;
                        }
                    }
                }
            }
        }

        private static void DiableToolStripItem(ToolStripButton parentItem, string shortcut)
        {
            if (parentItem.Name == shortcut)
            {
                parentItem.Enabled = true;
            }
        }

        private static void DiableMenuItem(ToolStripMenuItem parentItem, string shortcut)
        {
            if (parentItem != null)
            {
                foreach (var _item in parentItem.DropDownItems)
                {
                    if (_item.GetType() == typeof(ToolStripMenuItem))
                    {
                        DiableMenuItem((ToolStripMenuItem)_item, shortcut);
                    }
                }
                if (parentItem.Name == shortcut)
                {
                    parentItem.Enabled = true;
                }
            }
        }

        /// <summary>
        /// Disable some functions that user not allow using
        /// </summary>
        public static void EnableFuncAllowUsing(Panel pnlMain, string ctrlName)
        {
            DisableCtrls(pnlMain, ctrlName);
        }
    }
}
